﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using XCI.Data.Entity;
using XCI.Extensions;
using XCI.Windows.Controls;
using XCI.Windows.Helper;

namespace XCI.Windows.Plugin.GridSetting.UI
{
    [ToolboxItem(false)]
    public partial class BaseGridColumns : BaseXtraUserControl
    {
        protected GridControl MainGrid { get { return gridControl; } }
        protected GridView MainView { get { return gridView1; } }

        /// <summary>
        /// 列名下拉框
        /// </summary>
        protected RepositoryItemComboBox ColumnNameComboBox { get { return comboxFieldName; } }

        /// <summary>
        /// 列标题下拉框
        /// </summary>
        protected RepositoryItemComboBox ColumnCaptionComboBox { get { return comboxCaption; } }

        public BaseGridColumns()
        {
            InitializeComponent();
            //propertyGridGridColumn.PropertyGrid.OptionsView.ShowRootCategories = false;
        }

        protected void LoadFiledName()
        {
            EntityData meta = GetMetadata();
            if (meta == null) return;

            ColumnNameComboBox.Items.BeginUpdate();
            ColumnNameComboBox.Items.Clear();
            ColumnCaptionComboBox.Items.BeginUpdate();
            ColumnCaptionComboBox.Items.Clear();

            foreach (var item in meta.AllColumns)
            {
                var caption = item.Value.Caption;
                if (!string.IsNullOrEmpty(caption))
                {
                    ColumnCaptionComboBox.Items.Add(caption);
                }
            }
            meta.AllColumns.ForEach(p => ColumnNameComboBox.Items.Add(p.Key));

            ColumnNameComboBox.Items.EndUpdate();
            ColumnCaptionComboBox.Items.EndUpdate();
        }

        protected virtual EntityData GetMetadata()
        {
            return null;
        }

        protected virtual IList GetColumns()
        {
            return null;
        }

        protected virtual void NewColumn()
        {
        }
        protected virtual void RemoveColumn(object col)
        {
        }

        protected virtual void AutoBuildColumn()
        {
        }

        protected void Initialize()
        {
            LoadFiledName();
            MainGrid.DataSource = GetColumns();
        }

        private void SetObject()
        {
            var obj = gridView1.GetRow(gridView1.FocusedRowHandle);
            propertyGridGridColumn.SetObject(obj);
        }

        private void editSearchBox_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.ApplyFindFilter(((BaseEdit)sender).Text.Trim());
            SetObject();
        }

        private void gridViewGridColumn_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            SetObject();
            btnDelete.Enabled = gridView1.FocusedRowHandle > -1;
        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (gridView1.OptionsView.ShowIndicator && e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Info.ImageIndex = -1;
            }
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewColumn();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中的数据吗？") == DialogResult.Yes)
            {
                //gridView1.DeleteSelectedRows();
                RemoveColumn(gridView1.GetRow(gridView1.FocusedRowHandle));
            }
        }

        private void btnAutoGenerated_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowMessageYesNo("确定自动生成列吗？") != DialogResult.Yes) return;
            AutoBuildColumn();
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            if (gridView1.SelectedRowsCount > 1 || gridView1.SelectedRowsCount == 0)
            {
                gridView1.ClearSelection();
                gridView1.SelectRow(gridView1.FocusedRowHandle);
            }
        }
    }
}